import request from '@/utils/request'

export interface FinanceQuery {
  startDate?: string
  endDate?: string
  type?: string
  status?: string
  page: number
  pageSize: number
}

export interface FinanceRecord {
  id: string
  date: string
  code: string
  type: '收入' | '支出'
  amount: number
  category: string
  status: string
  approver: string
  remark?: string
}

export interface StatisticsData {
  totalIncome: {
    amount: number
    change: number
  }
  totalExpense: {
    amount: number
    change: number
  }
  monthlyIncome: {
    amount: number
    change: number
  }
  monthlyExpense: {
    amount: number
    change: number
  }
}

// 获取财务统计数据
export function getStatistics() {
  return request<StatisticsData>({
    url: '/api/finance/statistics',
    method: 'get'
  })
}

// 获取财务记录列表
export function getFinanceList(query: FinanceQuery) {
  return request<{ list: FinanceRecord[]; total: number }>({
    url: '/api/finance/list',
    method: 'get',
    params: query
  })
}

// 创建财务记录
export function createFinanceRecord(data: Partial<FinanceRecord>) {
  return request<FinanceRecord>({
    url: '/api/finance',
    method: 'post',
    data
  })
}

// 更新财务记录
export function updateFinanceRecord(id: string, data: Partial<FinanceRecord>) {
  return request<FinanceRecord>({
    url: `/api/finance/${id}`,
    method: 'put',
    data
  })
}

// 删除财务记录
export function deleteFinanceRecord(id: string) {
  return request({
    url: `/api/finance/${id}`,
    method: 'delete'
  })
}

// 获取财务类别列表
export function getCategoryList() {
  return request<Array<{ id: string; name: string }>>({
    url: '/api/finance/categories',
    method: 'get'
  })
}

// 审批财务记录
export function approveFinanceRecord(id: string, action: 'approve' | 'reject', remark?: string) {
  return request({
    url: `/api/finance/${id}/approve`,
    method: 'post',
    data: {
      action,
      remark
    }
  })
} 